package com.mercury.ec.module.air.dal.mysql.demandorder;

import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.mybatis.core.mapper.BaseMapperX;
import com.mercury.ec.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandorder.AirDemandOrderPageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandorder.AirDemandOrderRespVO;
import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandOrderDO;
import com.mercury.ec.module.system.dal.dataobject.dept.DeptDO;
import com.mercury.ec.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

/**
 * 机票需求订单 Mapper
 *
 * @author hesx
 */
@Mapper
public interface AirDemandOrderMapper extends BaseMapperX<AirDemandOrderDO> {

    default PageResult<AirDemandOrderRespVO> selectPage(AirDemandOrderPageReqVO reqVO) {
        return selectJoinPage(reqVO, AirDemandOrderRespVO.class, getMPJLambdaWrapperX(reqVO)
                .orderByDesc(AirDemandOrderDO::getId)
                .selectAll(AirDemandOrderDO.class)
                .selectAs(AdminUserDO::getNickname, AirDemandOrderRespVO::getApplyUserNickname)
                .selectAs("u2.nickname", AirDemandOrderRespVO::getBookUserNickname)
                .selectAs("u3.nickname", AirDemandOrderRespVO::getLockUserNickname)
                .selectAs("u4.nickname", AirDemandOrderRespVO::getFinallyQuoteUserNickname)
                .selectAs("u5.nickname", AirDemandOrderRespVO::getFinallyPricingUserNickname)
                .selectAs("u6.nickname", AirDemandOrderRespVO::getCancelUserNickname)
                .selectAs(DeptDO::getName, AirDemandOrderRespVO::getBookDeptName)
                .selectAs("d2.name", AirDemandOrderRespVO::getLockDeptName)
                .selectAs("d3.name", AirDemandOrderRespVO::getFinallyQuoteDeptName)
                .selectAs("d4.name", AirDemandOrderRespVO::getFinallyPricingDeptName)
                .selectAs("d5.name", AirDemandOrderRespVO::getCancelDeptName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, AirDemandOrderDO::getApplyUserId)
                .leftJoin(AdminUserDO.class, "u2", AdminUserDO::getId, AirDemandOrderDO::getBookUserId)
                .leftJoin(AdminUserDO.class, "u3", AdminUserDO::getId, AirDemandOrderDO::getLockUserId)
                .leftJoin(AdminUserDO.class, "u4", AdminUserDO::getId, AirDemandOrderDO::getFinallyQuoteUserId)
                .leftJoin(AdminUserDO.class, "u5", AdminUserDO::getId, AirDemandOrderDO::getFinallyPricingUserId)
                .leftJoin(AdminUserDO.class, "u6", AdminUserDO::getId, AirDemandOrderDO::getCancelUserId)
                .leftJoin(DeptDO.class, DeptDO::getId, AirDemandOrderDO::getBookDeptId)
                .leftJoin(DeptDO.class, "d2", DeptDO::getId, AirDemandOrderDO::getLockDeptId)
                .leftJoin(DeptDO.class, "d3", DeptDO::getId, AirDemandOrderDO::getFinallyQuoteDeptId)
                .leftJoin(DeptDO.class, "d4", DeptDO::getId, AirDemandOrderDO::getFinallyPricingDeptId)
                .leftJoin(DeptDO.class, "d5", DeptDO::getId, AirDemandOrderDO::getCancelDeptId)
        );
    }

    default List<Map<String, Object>> selectAirDemandOrderCountByStatus(AirDemandOrderPageReqVO reqVO) {
        return selectJoinMaps(getMPJLambdaWrapperX(reqVO)
                .selectAs(AirDemandOrderDO::getStatus, AirDemandOrderDO::getStatus)
                .selectCount(AirDemandOrderDO::getId, "count")
                .groupBy(AirDemandOrderDO::getStatus)
        );
    }

    default MPJLambdaWrapperX<AirDemandOrderDO> getMPJLambdaWrapperX(AirDemandOrderPageReqVO reqVO) {
        return new MPJLambdaWrapperX<AirDemandOrderDO>()
                .likeIfPresent(AirDemandOrderDO::getNo, reqVO.getNo())
                .eqIfPresent(AirDemandOrderDO::getCustomerId, reqVO.getCustomerId())
                .likeIfPresent(AirDemandOrderDO::getCustomerNo, reqVO.getCustomerNo())
                .likeIfPresent(AirDemandOrderDO::getCustomerName, reqVO.getCustomerName())
                .eqIfPresent(AirDemandOrderDO::getApplyUserId, reqVO.getApplyUserId())
                .likeIfPresent(AirDemandOrderDO::getFlightInfo, reqVO.getFlightInfo())
                .eqIfPresent(AirDemandOrderDO::getBookUserId, reqVO.getBookUserId())
                .eqIfPresent(AirDemandOrderDO::getBookDeptId, reqVO.getBookDeptId())
                .betweenIfPresent(AirDemandOrderDO::getBookTime, reqVO.getBookTime())
                .eqIfPresent(AirDemandOrderDO::getLocked, reqVO.getLocked())
                .eqIfPresent(AirDemandOrderDO::getLockUserId, reqVO.getLockUserId())
                .eqIfPresent(AirDemandOrderDO::getLockDeptId, reqVO.getLockDeptId())
                .betweenIfPresent(AirDemandOrderDO::getLockTime, reqVO.getLockTime())
                .eqIfPresent(AirDemandOrderDO::getFinallyQuoteUserId, reqVO.getFinallyQuoteUserId())
                .eqIfPresent(AirDemandOrderDO::getFinallyQuoteDeptId, reqVO.getFinallyQuoteDeptId())
                .betweenIfPresent(AirDemandOrderDO::getFinallyQuoteTime, reqVO.getFinallyQuoteTime())
                .eqIfPresent(AirDemandOrderDO::getFinallyPricingUserId, reqVO.getFinallyPricingUserId())
                .eqIfPresent(AirDemandOrderDO::getFinallyPricingDeptId, reqVO.getFinallyPricingDeptId())
                .betweenIfPresent(AirDemandOrderDO::getFinallyPricingTime, reqVO.getFinallyPricingTime())
                .eqIfPresent(AirDemandOrderDO::getCancelRuleId, reqVO.getCancelRuleId())
                .eqIfPresent(AirDemandOrderDO::getCancelUserId, reqVO.getCancelUserId())
                .eqIfPresent(AirDemandOrderDO::getCancelDeptId, reqVO.getCancelDeptId())
                .betweenIfPresent(AirDemandOrderDO::getCancelTime, reqVO.getCancelTime())
                .eqIfPresent(AirDemandOrderDO::getCancelCause, reqVO.getCancelCause())
                .eqIfPresent(AirDemandOrderDO::getContact, reqVO.getContact())
                .eqIfPresent(AirDemandOrderDO::getContactPhone, reqVO.getContactPhone())
                .eqIfPresent(AirDemandOrderDO::getStatus, reqVO.getStatus())
                .likeIfPresent(AirDemandOrderDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(AirDemandOrderDO::getCreateTime, reqVO.getCreateTime());
    }

    default AirDemandOrderDO selectByNo(String no) {
        return selectOne(AirDemandOrderDO::getNo, no);
    }

    default AirDemandOrderRespVO selectAirDemandOrderById(Long id) {
        return selectJoinOne(AirDemandOrderRespVO.class, new MPJLambdaWrapperX<AirDemandOrderDO>().eq(AirDemandOrderDO::getId, id)
                .selectAll(AirDemandOrderDO.class)
                .selectAs(AdminUserDO::getNickname, AirDemandOrderRespVO::getApplyUserNickname)
                .selectAs("u2.nickname", AirDemandOrderRespVO::getBookUserNickname)
                .selectAs("u3.nickname", AirDemandOrderRespVO::getLockUserNickname)
                .selectAs("u4.nickname", AirDemandOrderRespVO::getFinallyQuoteUserNickname)
                .selectAs("u5.nickname", AirDemandOrderRespVO::getFinallyPricingUserNickname)
                .selectAs("u6.nickname", AirDemandOrderRespVO::getCancelUserNickname)
                .selectAs(DeptDO::getName, AirDemandOrderRespVO::getBookDeptName)
                .selectAs("d2.name", AirDemandOrderRespVO::getLockDeptName)
                .selectAs("d3.name", AirDemandOrderRespVO::getFinallyQuoteDeptName)
                .selectAs("d4.name", AirDemandOrderRespVO::getFinallyPricingDeptName)
                .selectAs("d5.name", AirDemandOrderRespVO::getCancelDeptName)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, AirDemandOrderDO::getApplyUserId)
                .leftJoin(AdminUserDO.class, "u2", AdminUserDO::getId, AirDemandOrderDO::getBookUserId)
                .leftJoin(AdminUserDO.class, "u3", AdminUserDO::getId, AirDemandOrderDO::getLockUserId)
                .leftJoin(AdminUserDO.class, "u4", AdminUserDO::getId, AirDemandOrderDO::getFinallyQuoteUserId)
                .leftJoin(AdminUserDO.class, "u5", AdminUserDO::getId, AirDemandOrderDO::getFinallyPricingUserId)
                .leftJoin(AdminUserDO.class, "u6", AdminUserDO::getId, AirDemandOrderDO::getCancelUserId)
                .leftJoin(DeptDO.class, DeptDO::getId, AirDemandOrderDO::getBookDeptId)
                .leftJoin(DeptDO.class, "d2", DeptDO::getId, AirDemandOrderDO::getLockDeptId)
                .leftJoin(DeptDO.class, "d3", DeptDO::getId, AirDemandOrderDO::getFinallyQuoteDeptId)
                .leftJoin(DeptDO.class, "d4", DeptDO::getId, AirDemandOrderDO::getFinallyPricingDeptId)
                .leftJoin(DeptDO.class, "d5", DeptDO::getId, AirDemandOrderDO::getCancelDeptId)
        );
    }
}
